package com.ruben.simplescaffold.wrappers;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.ruben.simplescaffold.pojo.common.BaseEntity;

import java.util.List;
import java.util.Objects;

/**
 * Wrappers封装，抽取便于复用
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/5/8 0008 10:31
 */
public class CommonWrappers {
    private CommonWrappers() {
    }

    /**
     * 获取in wrapper 替代 Wrappers.lambdaQuery(new Course()).in(Course::getCourseId, courseIds)
     *
     * @param inCondition in条件
     * @param dataList    数据
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<T>
     * @author <achao1441470436@gmail.com>
     * @since 2021/5/8 0008 10:35
     */
    public static <T extends BaseEntity, R> LambdaQueryWrapper<T> inWrapper(SFunction<T, R> inCondition, List<R> dataList) {
        return new LambdaQueryWrapper<T>().in(inCondition, dataList);
    }

    /**
     * 类型查询枚举
     *
     * @param typeCondition 类型条件
     * @param typeEnum      类型枚举
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<T>
     * @author <achao1441470436@gmail.com>
     * @since 2021/6/25 9:45
     */
    public static <T extends BaseEntity> LambdaQueryWrapper<T> typeWrapper(SFunction<T, Integer> typeCondition, Enum<?> typeEnum) {
        return new LambdaQueryWrapper<T>().eq(typeCondition, typeEnum.ordinal());
    }

    /**
     * 永远返回false，封装select(i->false)，
     *
     * @param tableFieldInfo whatever
     * @return boolean false
     * @author <achao1441470436@gmail.com>
     * @since 2021/7/7 10:00
     */
    public static boolean onlyIds(TableFieldInfo tableFieldInfo) {
        // 变量用一下，不然又要提示变量没使用
        return Objects.isNull(tableFieldInfo);
    }
}
